<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        nav ul {
            width: 400px;
            height: 30px;
            background-color: gray;
            display: flex;
        }
        nav li {
            list-style: none;
            width:80px;
            height:30px;
            line-height: 30px;
            text-decoration: none;
            text-align: center;
        }

        .active{
            background-color: blue;
            list-style: none;
            width:80px;
            height:30px;
            line-height: 30px;
            text-decoration: none;
            text-align: center;
        }
        section{
            height: 300px;
            width: 400px;
            background-color: red;
             position: absolute;
            top: 30px; 
        }

    </style>
</head>

<body>

    <nav id="nav">
        <ul>
            <li class="act">篮球</li>
            <li>棒球</li>
            <li>篮球</li>
            <li>篮球</li>
        </ul>
    </nav>

    <div id="container">
        <section class="tab" >篮球</section>
        <section class="tab" >棒球</section>
        <section class="tab" >羽毛球</section>
        <section class="tab" >乒乓球</section>
    </div>
   
</body>

</html>
<script>
    window.onload = function () {
      var oList = document.querySelectorAll("ul>li") 
      var oSec = document.querySelectorAll("div>section")
      console.log(oSec)
       for(var i=0;i<oList.length;i++){
           oList[i].index=i
           oList[i].onclick=function(){
               for(var j=0;j<oList.length;j++){
                 oList[j].className=''
                 oSec[j].style.display="none"
               }
               this.className='active'
               oSec[this.index].style.display="block"
           }
           
       }
    };
</script>